// var body_server_url = "http://192.168.31.175:1314/api/complex/BodyData";
var body_server_url = "http://121.40.18.23:1314/api/complex/BodyData";
var bodyPage;
var items = new observableArray();
var timeFactory = require("~/user-module/timeFactory.js").timeFactory;
timeFactory = new timeFactory();

var pain = ["无", "轻微 ", "非常", "剧烈！"];
var selectView;
var bodyData = new observable({
    starttime: "",
    endtime: "",
    cycle: "",
    period: "",
    pain: "",
    status: ""
});
var HistoryData = new observable({
    starttime: "",
    endtime: "",
    cycle: "",
    period: "",
    pain: "",
    status: ""
});
exports.bodyLoaded = args => {
    bodyPage = args.object;
    bodyPage.bindingContext = new observable({
        items: pain,
        bodyData: bodyData,
        HistoryData: HistoryData,
        selectedIndex: 1
    });
    getMens();
    selectView = bodyPage.getViewById("pain");
};

var _create_new = true;
var _bodyData;
exports.postData = args => {
    // console.log(selectView.get("selectedIndex"));
    dialogs.confirm(_create_new ? "经期开始？" : "经期结束？").then(function(result) {
        if (result) {
            _create_new ? startMens() : endMens(_bodyData);
        };
    });
};

var getMens = () => {
    http.getJSON({
        url: body_server_url,
        method: "GET",
        headers: { "Content-Type": "application/json" }
    }).then(function(response) {

        var len = response.length;
        // console.log(len)
        if (len > 1) {
            temp(response[1]);
            // console.log(JSON.stringify(response[1]));
            // console.log(JSON.stringify(response[0]));
            HistoryData.starttime = response[0].starttime ? timeFactory.getDate("YYYY-MM-DD hh:mm:ss", response[0].starttime) : 0;
            HistoryData.endtime = response[0].endtime ? timeFactory.getDate("YYYY-MM-DD hh:mm:ss", response[0].endtime) : 0;
            HistoryData.cycle = response[0].cycle;
            HistoryData.period = response[0].period;
            HistoryData.pain = response[0].pain;
            HistoryData.status = response[0].status;
        } else {
            temp(response[0]);
        };
    });
};

var startMens = () => {
    http.getJSON({
        url: body_server_url,
        method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ starttime: new Date().getTime(), pain: pain[selectView.get("selectedIndex")] })
    }).then(function(response) {

        // temp(response);
        getMens();
    });
};

var endMens = params => {
    // console.log("put")
    http.getJSON({
        url: body_server_url,
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify(params)
    }).then(function(response) {
        // temp(response);
        getMens();
    });
};

var temp = params => {
    if (params) {
        bodyData.starttime = params.starttime ? timeFactory.getDate("YYYY-MM-DD hh:mm:ss", params.starttime) : 0;
        bodyData.endtime = params.endtime ? timeFactory.getDate("YYYY-MM-DD hh:mm:ss", params.endtime) : 0;
        bodyData.cycle = params.cycle;
        bodyData.period = params.period;
        bodyData.pain = params.pain;
        bodyData.status = params.status;
    };
    _bodyData = params;
    params.endtime ? _create_new = true : (params.starttime ? _create_new = false : _create_new = true)
}